#pragma once

#define CV_WINDOW_AUTOSIZE 1

#include <opencv2/opencv.hpp>
using namespace cv;

namespace opencv_coordinate 
{

    void arrow(cv::Mat &img, cv::Point p1, cv::Point p2, cv::Scalar &color, int thickness, int alpha);

    int run()
    {
        Mat_<uchar> CSYS;
        CSYS.create(600, 600); // 定义灰度图像大小
        Scalar color(0, 0, 0);
        Scalar color_word(1, 1, 1);
        line(CSYS, Point(100, 300), Point(500, 300), color, 2);                    // X轴
        line(CSYS, Point(300, 100), Point(300, 500), color, 2);                    // Y轴
                                                                                   /*	箭头函数稍后给出		*/
        arrow(CSYS, Point(484, 300), Point(500, 300), color, 2, 30);               // X轴箭头
        arrow(CSYS, Point(300, 116), Point(300, 100), color, 2, 30);               // Y轴箭头
        putText(CSYS, "t", Point(490, 330), FONT_HERSHEY_PLAIN, 2, color_word, 2); // 坐标轴文本
        putText(CSYS, "O", Point(270, 330), FONT_HERSHEY_PLAIN, 2, color_word, 2);
        putText(CSYS, "f(x)", Point(310, 130), FONT_HERSHEY_PLAIN, 2, color_word, 2);

        line(CSYS, Point(220, 300), Point(220, 220), color, 2); // 函数划线
        line(CSYS, Point(220, 220), Point(380, 220), color, 2);
        line(CSYS, Point(380, 220), Point(380, 300), color, 2);
        putText(CSYS, "t0", Point(370, 330), FONT_HERSHEY_PLAIN, 1, color_word, 1); // 函数文本
        putText(CSYS, "-t0", Point(200, 330), FONT_HERSHEY_PLAIN, 1, color_word, 1);
        putText(CSYS, " A", Point(310, 210), FONT_HERSHEY_PLAIN, 1, color_word, 1);

        // namedWindow("坐标系", CV_WINDOW_AUTOSIZE);
        imshow("loc", CSYS);
        cv::waitKey(0);
        return 0;
    }

    void arrow(cv::Mat &img, cv::Point p1, cv::Point p2, cv::Scalar &color, int thickness, int alpha)
    {
        Point arrow;
        const double PI = 3.1415926;
        float len = sqrt((float)((p2.y - p1.y) * (p2.y - p1.y) + (p1.x - p2.x) * (p1.x - p2.x))); // 计算俩点间距离
        double angle = atan2((double)(p2.y - p1.y), (double)(p2.x - p1.x));                       //**注意这里角度的计算结果为弧度制**
        line(img, p1, p2, color, thickness);

        /*下边画箭头的俩根斜线*/
        arrow.x = (int)(p2.x - len * cos(angle + PI * alpha / 180));
        arrow.y = (int)(p2.y - len * sin(angle + PI * alpha / 180));
        line(img, arrow, p2, color, thickness);
        arrow.x = (int)(p2.x - len * cos(angle - PI * alpha / 180));
        arrow.y = (int)(p2.y - len * sin(angle - PI * alpha / 180));
        line(img, arrow, p2, color, thickness);
    }

}
